package cn.scy.base;

import cn.scy.constant.ErrorCode;
import lombok.Data;

/**
 * 请求返回对象
 *
 * @author scy
 */
@Data
public class ResponseDTO<T> {

    public static final int OK_CODE = 0;

    public static final String OK_MSG = "操作成功";

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 是否成功
     */
    private Boolean ok;

    /**
     * 返回数据
     */
    private T data;

    public ResponseDTO(Integer code, boolean ok, String msg, T data) {
        this.code = code;
        this.ok = ok;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResponseDTO<T> ok() {
        return new ResponseDTO<>(OK_CODE, true, OK_MSG, null);
    }

    public static <T> ResponseDTO<T> ok(T data) {
        return new ResponseDTO<>(OK_CODE, true, OK_MSG, data);
    }

    public static <T> ResponseDTO<T> okMsg(String msg) {
        return new ResponseDTO<>(OK_CODE, true, msg, null);
    }

    public static <T> ResponseDTO<T> userErrorParam() {
        return new ResponseDTO<>(ErrorCode.ERROR_PARAM.getCode(), false, ErrorCode.ERROR_PARAM.getMsg(), null);
    }

    public static <T> ResponseDTO<T> error(Integer errorCode, String errorMsg) {
        return new ResponseDTO<>(errorCode, false, errorMsg, null);
    }

    public static <T> ResponseDTO<T> error(ErrorCode errorCode) {
        return new ResponseDTO<>(errorCode.getCode(), false, errorCode.getMsg(), null);
    }

    public static <T> ResponseDTO<T> errorData(ErrorCode errorCode, T data) {
        return new ResponseDTO<>(errorCode.getCode(), false, errorCode.getMsg(), data);
    }

    public static <T> ResponseDTO<T> error(ResponseDTO<?> responseDTO) {
        return new ResponseDTO<>(responseDTO.getCode(), responseDTO.getOk(), responseDTO.getMsg(), null);
    }

}
